package com.fhzz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fhzz.mapper.LeaveApplyMapper;
import com.fhzz.model.LeaveApply;
import com.fhzz.service.LeaveService;
import com.fhzz.service.WorkflowService;

@Service
public class LeaveServiceImpl implements LeaveService{
	
	@Autowired
	private LeaveApplyMapper leaveApplyMapper;
	
	@Autowired
	private IdentityService identityservice;
	
	@Autowired
	private WorkflowService workflowService;
	
	

	@Override
	@Transactional
	public ProcessInstance startWorkflow(LeaveApply apply, String userid, 
			Map<String, Object> variables) {
		//本地保存
		apply.setApply_time(new Date().toString());
		apply.setUser_id(userid);
		leaveApplyMapper.save(apply);
		
		String businesskey=String.valueOf(apply.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
		
		identityservice.setAuthenticatedUserId(userid);
		
		ProcessInstance instance = workflowService.startProcessInstanceByKey("leave", businesskey, variables);
		System.out.println(businesskey);
		
		String instanceid=instance.getId();
		apply.setProcess_instance_id(instanceid);
		
		leaveApplyMapper.updateByPrimaryKey(apply);
		
		return instance;
	}

	@Override
	public List<LeaveApply> getPageDeptTask(String userid, int firstrow, int rowcount) {
		List<LeaveApply> results=new ArrayList<LeaveApply>();
		List<Task> tasks = workflowService.getTaskCandidateGroup("部门经理", firstrow, rowcount);
		tasks.forEach(task -> {
			String instanceid = task.getProcessInstanceId();
			ProcessInstance ins = workflowService.getInstanceById(instanceid);
			String businesskey = ins.getBusinessKey();
			LeaveApply a = leaveApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
			a.setTask(task);
			results.add(a);
		});
		return results;
	}

	@Override
	public int getAllDeptTask(String userid) {
		return workflowService.getTaskCandidateGroupCount("部门经理");
	}

	@Override
	public LeaveApply getleave(int id) {
		LeaveApply leave=leaveApplyMapper.getLeaveApply(id);
		return leave;
	}

	@Override
	public List<LeaveApply> getPageHrTask(String userid, int firstrow, int rowcount) {
		List<LeaveApply> results=new ArrayList<LeaveApply>();
		List<Task> tasks = workflowService.getTaskCandidateGroup("人事", firstrow, rowcount);
		tasks.forEach(task -> {
			String instanceid=task.getProcessInstanceId();
			ProcessInstance ins = workflowService.getInstanceById(instanceid);
			String businesskey = ins.getBusinessKey();
			LeaveApply a = leaveApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
			a.setTask(task);
			results.add(a);
		});
		return results;
	}

	@Override
	public int getAllHrTask(String userid) {
		return workflowService.getTaskCandidateGroupCount("人事");
	}

	@Override
	public List<LeaveApply> getPageXjTask(String userid, int firstrow, int rowcount) {
		List<LeaveApply> results=new ArrayList<LeaveApply>();
		List<Task> tasks = workflowService.getTaskCandidateOrAssigned(userid, "销假", firstrow, rowcount);
		tasks.forEach(task -> {
			String instanceid=task.getProcessInstanceId();
			ProcessInstance ins = workflowService.getInstanceById(instanceid);
			String businesskey=ins.getBusinessKey();
			LeaveApply a = leaveApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
			a.setTask(task);
			results.add(a);
		});
		return results;
	}

	@Override
	public int getAllXjTask(String userid) {
		return workflowService.getTaskCandidateGroupCount("人事");
	}

	@Override
	public List<LeaveApply> getPageUpdateApplyTask(String userid, int firstrow, int rowcount) {
		List<LeaveApply> results=new ArrayList<LeaveApply>();
		List<Task> tasks = workflowService.getTaskCandidateOrAssigned(userid, "调整申请", firstrow, rowcount);
		tasks.forEach(task -> {
			String instanceid=task.getProcessInstanceId();
			ProcessInstance ins = workflowService.getInstanceById(instanceid);
			String businesskey=ins.getBusinessKey();
			LeaveApply a = leaveApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
			a.setTask(task);
			results.add(a);
		});
		return results;
	}

	@Override
	public int getAllUpdateApplyTask(String userid) {
		
		return workflowService.getTaskCandidateOrAssignedCount(userid, "销假调整");
	}

	@Override
	@Transactional
	public void completeReportBack(String taskid, String realstart_time, String realend_time) {
		Task task = workflowService.getTaskById(taskid);
		String instanceid=task.getProcessInstanceId();
		ProcessInstance ins = workflowService.getInstanceById(instanceid);
		String businesskey=ins.getBusinessKey();
		LeaveApply a = leaveApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
		a.setReality_start_time(realstart_time);
		a.setReality_end_time(realend_time);
		leaveApplyMapper.updateByPrimaryKey(a);
		workflowService.completeTask(taskid);
	}

	@Override
	@Transactional
	public void updateComplete(String taskid, LeaveApply leave, String reapply) {
		Task task = workflowService.getTaskById(taskid);
		String instanceid=task.getProcessInstanceId();
		ProcessInstance ins = workflowService.getInstanceById(instanceid);
		String businesskey=ins.getBusinessKey();
		LeaveApply a = leaveApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
		a.setLeave_type(leave.getLeave_type());
		a.setStart_time(leave.getStart_time());
		a.setEnd_time(leave.getEnd_time());
		a.setReason(leave.getReason());
		Map<String,Object> variables=new HashMap<String,Object>();
		variables.put("reapply", reapply);
		
		if(reapply.equals("true")){//重新到部门经理
			leaveApplyMapper.updateByPrimaryKey(a);
		}
		workflowService.completeTask(taskid, variables);
	}

	
	
}
